<template>
    <div class="login">
        <div class="form">
            <div class="title">
                智慧点餐平台
            </div>

            <el-form :model="formData" ref="formRef" :rules="rules">
                <el-form-item prop="account">
                    <el-input v-model="formData.account" placeholder="请输入账号">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <UserFilled color="#fff" />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item  prop="password">
                    <el-input v-model="formData.password" type="password" show-password placeholder="请输入密码">
                        <template #prefix>
                            <el-icon class="el-input__icon">
                                <Lock color="#fff" />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" style="width: 100%;" @click="login">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script setup>
import { reactive,ref } from 'vue'
import { useRouter } from 'vue-router'
import { UserFilled, Lock } from '@element-plus/icons-vue'
import { checkLogin } from '@/api/account.js'
import { ElMessage } from 'element-plus'

const router = useRouter()

const formData = reactive({
    account: '',
    password: ''
})

const formRef = ref()

const login = () => {

    formRef.value.validate(async (res) => {
        if (res) {

            let { code, msg, id, role, token } = await checkLogin(formData);

            if (code == 0) {
                sessionStorage.setItem('user', JSON.stringify({ id, role, token }))
                ElMessage.success(msg)
                router.push('/home')
            } else {
                ElMessage.error(msg)
            }
        }
    })

}

const validateAccount = (rule, value, callback) => {
  let reg = /^([a-z]|[0-9]){4,11}$/
  if (reg.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的账号'))
  }

}
const validatePassword = (rule, value, callback) => {
  let reg = /^([a-z]|[0-9]){4,11}$/
  if (reg.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的密码'))
  }
}
const rules = reactive({
  account: [
    { validator: validateAccount, trigger: 'blur' }
  ],
  password:[
    {validator: validatePassword, trigger: 'blur' }
  ]
})

</script>

<style scoped lang="scss">
.login {
    width: 100%;
    height: 100%;
    background-color: #2d3a4b;
}

.form {
    width: 300px;
    height: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.title {
    width: 100%;
    height: 50px;
    color: #fff;
    font-size: 20px;
    line-height: 50px;
    text-align: center;
}

:deep(.el-input__wrapper) {
    background-color: #242e3c;
}

:deep(.el-input__inner) {
    color: #fff;
}
</style>